// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Config Screen Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 7 : TYPE_CONFIGSCREEN

private alias saveRAM[496] : saved.BossMusic //7C0 SData.bin
private alias saveRAM[497] : saved.Character //7C4 SData.bin
private alias saveRAM[498] : saved.TimeLimit //7C8 SData.bin
private alias saveRAM[499] : saved.superMode //7CC SData.bin
private alias saveRAM[500] : bossAttackTime.Sonic //7D0 SData.bin
private alias saveRAM[501] : bossAttackTime.SonicTails //7D4 SData.bin
private alias saveRAM[502] : bossAttackTime.Tails //7D8 SData.bin
private alias saveRAM[503] : bossAttackTime.Knuckles //7DC SData.bin
private alias saveRAM[504] : bossAttackTime.KnucklesTails //7E0 SData.bin

private alias object.value4 : character.MenuCoice


// Static Values

// Tables

event ObjectMain
	switch object.state
	case 0
		object.inkEffect = INK_ALPHA
		object.alpha = 0
		object.drawOrder = 6
		object.state++
		break
	case 1
		temp0 = 198
		temp0 -= object.value1
		temp0 >>= 3
		object.value1 += temp0
		temp0 = 216
		temp0 -= object.value2
		temp0 >>= 3
		object.value2 += temp0
		if object.value1 >= 188
			object.value1 = 188
			object.value2 = 208
			ReadSaveRAM()
			character.MenuCoice = saved.Character
				switch character.MenuCoice
				case 0
					stage.playerListPos = 0
					stage.player2Enabled = 1
					break
				case 1
					stage.playerListPos = 0
					stage.player2Enabled = 0
					break
				case 2
					stage.playerListPos = 1
					stage.player2Enabled = 0
					break
				case 3
					stage.playerListPos = 2
					stage.player2Enabled = 0
					break
				case 4
					stage.playerListPos = 2
					stage.player2Enabled = 1
					break
				end switch
			object.state++
		end if
		break
	case 2
		if object.alpha < 256
			object.alpha += 16
		else
			if options.physicalControls == 1
				object.value3 = 1
			else
				object.value3 = 0
			end if
			object.state++
		end if
		break
	case 3
		if options.physicalControls == 1
			if inputPress.up == 1
				object.value3--
				if object.value3 < 1
					object.value3 = 5
				end if
				PlaySfx(SfxName[Menu Move], 0)
			end if
			if inputPress.down == 1
				object.value3++
				if object.value3 > 5
					object.value3 = 1
				end if
				PlaySfx(SfxName[Menu Move], 0)
			end if
			switch object.value3
			case 1
				if inputPress.left == 1
					character.MenuCoice--
					if character.MenuCoice < 0
						character.MenuCoice = 4
					end if
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if inputPress.right == 1
					character.MenuCoice++
					if character.MenuCoice > 4
						character.MenuCoice = 0
					end if
					PlaySfx(SfxName[Menu Move], 0)
				end if
				switch character.MenuCoice
				case 0
					stage.playerListPos = 0
					stage.player2Enabled = 1
					break
				case 1
					stage.playerListPos = 0
					stage.player2Enabled = 0
					break
				case 2
					stage.playerListPos = 1
					stage.player2Enabled = 0
					break
				case 3
					stage.playerListPos = 2
					stage.player2Enabled = 0
					break
				case 4
					stage.playerListPos = 2
					stage.player2Enabled = 1
					break
				end switch
				break
			case 2
				if inputPress.left == 1
					options.bossMusic--
					if options.bossMusic < 0
						options.bossMusic = 9
					end if
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if inputPress.right == 1
					options.bossMusic++
					if options.bossMusic > 9
						options.bossMusic = 0
					end if
					PlaySfx(SfxName[Menu Move], 0)
				end if
				break
			case 3
				if inputPress.left == 1
					options.timeLimit ^= 1
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if inputPress.right == 1
					options.timeLimit ^= 1
					PlaySfx(SfxName[Menu Move], 0)
				end if
				break
			case 4
				break
			case 5
				if inputPress.buttonA == 1
					object.state = 4
					PlaySfx(SfxName[Menu Select], 0)
				end if
				if inputPress.start == 1
					object.state = 4
					PlaySfx(SfxName[Menu Select], 0)
				end if
				break
			end switch
			if inputPress.buttonB == 1
				object.state = 5
				PlaySfx(SfxName[Menu Back], 0)
			end if
			if object.state == 3
				CheckTouchRect(0, 0, screen.xsize, screen.ysize)
				if checkResult > -1
					options.physicalControls = 0
					object.value3 = 0
				end if
			end if
		else
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult > -1
				temp0 = screen.xcenter
				temp0 -= 26
				temp1 = temp0
				temp1 += 38
				CheckTouchRect(temp0, 36, temp1, 68)
				if checkResult > -1
					object.value5 = 1
				else
					object.value5 = 0
				end if
				CheckTouchRect(temp0, 76, temp1, 108)
				if checkResult > -1
					object.value6 = 1
				else
					object.value6 = 0
				end if
				temp0 = screen.xcenter
				temp0 += 58
				temp1 = temp0
				temp1 += 38
				CheckTouchRect(temp0, 36, temp1, 68)
				if checkResult > -1
					object.value7 = 1
				else
					object.value7 = 0
				end if
				CheckTouchRect(temp0, 76, temp1, 108)
				if checkResult > -1
					object.value8 = 1
				else
					object.value8 = 0
				end if
				temp0 = screen.xcenter
				temp0 -= 16
				temp1 = temp0
				temp1 += 54
				CheckTouchRect(temp0, 116, temp1, 148)
				if checkResult > -1
					object.value9 = 1
				else
					object.value9 = 0
				end if
				temp0 = screen.xcenter
				temp0 += 38
				temp1 = temp0
				temp1 += 54
				CheckTouchRect(temp0, 116, temp1, 148)
				if checkResult > -1
					object.value11 = 1
				else
					object.value11 = 0
				end if
				temp0 = screen.xcenter
				temp0 -= 27
				temp1 = temp0
				temp1 += 54
				CheckTouchRect(temp0, 192, temp1, 218)
				if checkResult > -1
					object.value13 = 1
				else
					object.value13 = 0
				end if
			else
				if object.value5 == 1
					object.value5 = 0
					character.MenuCoice--
					if character.MenuCoice < 0
						character.MenuCoice = 4
					end if
					switch character.MenuCoice
					case 0
						stage.playerListPos = 0
						stage.player2Enabled = 1
						break
					case 1
						stage.playerListPos = 0
						stage.player2Enabled = 0
						break
					case 2
						stage.playerListPos = 1
						stage.player2Enabled = 0
						break
					case 3
						stage.playerListPos = 2
						stage.player2Enabled = 0
						break
					case 4
						stage.playerListPos = 2
						stage.player2Enabled = 1
						break
					end switch
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if object.value6 == 1
					object.value6 = 0
					options.bossMusic--
					if options.bossMusic < 0
						options.bossMusic = 9
					end if
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if object.value7 == 1
					object.value7 = 0
					character.MenuCoice++
					if character.MenuCoice > 4
						character.MenuCoice = 0
					end if
					switch character.MenuCoice
					case 0
						stage.playerListPos = 0
						stage.player2Enabled = 1
						break
					case 1
						stage.playerListPos = 0
						stage.player2Enabled = 0
						break
					case 2
						stage.playerListPos = 1
						stage.player2Enabled = 0
						break
					case 3
						stage.playerListPos = 2
						stage.player2Enabled = 0
						break
					case 4
						stage.playerListPos = 2
						stage.player2Enabled = 1
						break
					end switch
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if object.value8 == 1
					object.value8 = 0
					options.bossMusic++
					if options.bossMusic > 9
						options.bossMusic = 0
					end if
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if object.value9 == 1
					object.value9 = 0
					options.timeLimit  = 1
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if object.value11 == 1
					object.value11 = 0
					options.timeLimit  = 0
					PlaySfx(SfxName[Menu Move], 0)
				end if
				if object.value13 == 1
					object.value13 = 0
					object.state = 4
					PlaySfx(SfxName[Menu Select], 0)
				end if
			end if
			if inputPress.buttonB == 1
				object.state = 5
				PlaySfx(SfxName[Menu Back], 0)
			end if
			if object.state == 3
				if inputPress.up == 1
					options.physicalControls = 1
					object.value3 = 5
				end if
				if inputPress.down == 1
					options.physicalControls = 1
					object.value3 = 1
				end if
			end if
		end if
		break
	case 4
		if object.value0 < 32
			object.value0++
		else
			foreach (TypeName[Menu Control], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].state = 3
				options.bossGame = 1
				saved.BossMusic = options.bossMusic
				saved.Character = character.MenuCoice
				saved.TimeLimit = options.timeLimit
				WriteSaveRAM()
				player.lives = 255
				player.continues = 0
			next
		end if
		break
	case 5
		if object.value0 < 32
			object.value0++
		else
			object.value0 = 0
			object.state++
		end if
		break
	case 6
		if object.alpha > 0
			object.alpha -= 16
		else
			object.state++
		end if
		break
	case 7
		temp0 = 0
		temp0 -= object.value1
		temp0 >>= 3
		object.value1 += temp0
		temp0 = 0
		temp0 -= object.value2
		temp0 >>= 3
		object.value2 += temp0
		if object.value1 < 4
			object.value1 = 0
			object.value2 = 0
			MenuControl_value2 = 0
				saved.BossMusic = options.bossMusic
				saved.Character = character.MenuCoice
				saved.TimeLimit = options.timeLimit
				WriteSaveRAM()
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break
	end switch
end event


event ObjectDraw
	temp0 = object.value1
	temp0 >>= 1
	FlipSign(temp0)
	temp0 += screen.xcenter
	temp1 = object.value2
	temp1 >>= 1
	FlipSign(temp1)
	temp1 += screen.ycenter
	DrawRect(temp0, temp1, object.value1, object.value2, 0, 0, 96, 255)
	temp0 += 2
	temp1 += 2
	temp2 = object.value1
	temp2 -= 4
	temp3 = object.value2
	temp3 -= 4
	DrawRect(temp0, temp1, temp2, temp3, 0, 32, 192, 255)
	DrawSpriteScreenFX(0, FX_INK, screen.xcenter, 25)

	//bossAttackTime = stage.seconds
	//bossAttackTime *= 100
	//temp0 = stage.minutes
	//temp0 *= 0x1770
	//bossAttackTime += temp0
	//bossAttackTime += stage.milliSeconds
	switch character.MenuCoice
	case 0 //Sonic and Tails
		temp0 = bossAttackTime.SonicTails
		temp0 /= 0x1770
		stage.minutes = temp0
		temp0 = bossAttackTime.SonicTails
		temp1 = 0x1770
		temp1 *= stage.minutes
		temp0 -= temp1
		temp0 /= 100
		stage.seconds = temp0
		stage.milliseconds = bossAttackTime.SonicTails
		break
	case 1 //Sonic alone
		temp0 = bossAttackTime.Sonic
		temp0 /= 0x1770
		stage.minutes = temp0
		temp0 = bossAttackTime.Sonic
		temp1 = 0x1770
		temp1 *= stage.minutes
		temp0 -= temp1
		temp0 /= 100
		stage.seconds = temp0
		stage.milliseconds = bossAttackTime.Sonic
		break
	case 2 //Tails alone
		temp0 = bossAttackTime.Tails
		temp0 /= 0x1770
		stage.minutes = temp0
		temp0 = bossAttackTime.Tails
		temp1 = 0x1770
		temp1 *= stage.minutes
		temp0 -= temp1
		temp0 /= 100
		stage.seconds = temp0
		stage.milliseconds = bossAttackTime.Tails
		break
	case 3 //Knuckles alone
		temp0 = bossAttackTime.Knuckles
		temp0 /= 0x1770
		stage.minutes = temp0
		temp0 = bossAttackTime.Knuckles
		temp1 = 0x1770
		temp1 *= stage.minutes
		temp0 -= temp1
		temp0 /= 100
		stage.seconds = temp0
		stage.milliseconds = bossAttackTime.Knuckles
		break
	case 4 //Knuckles and Tails
		temp0 = bossAttackTime.KnucklesTails
		temp0 /= 0x1770
		stage.minutes = temp0
		temp0 = bossAttackTime.KnucklesTails
		temp1 = 0x1770
		temp1 *= stage.minutes
		temp0 -= temp1
		temp0 /= 100
		stage.seconds = temp0
		stage.milliseconds = bossAttackTime.KnucklesTails
		break
	endswitch

	if object.state == 3
		DrawNumbers(38, 230, 169, stage.minutes, 2, 8, 1)
		DrawNumbers(38, 250, 169, stage.seconds, 2, 8, 1)
		DrawNumbers(38, 270, 169, stage.milliseconds, 2, 8, 1)
	endif

	DrawSpriteScreenFX(48, FX_INK, 238, 169)
	DrawSpriteScreenFX(48, FX_INK, 258, 169)
	if object.value3 == 1
		DrawSpriteScreenFX(2, FX_INK, screen.xcenter, 49)
	else
		DrawSpriteScreenFX(1, FX_INK, screen.xcenter, 49)
	end if
	if object.value3 == 2
		DrawSpriteScreenFX(4, FX_INK, screen.xcenter, 89)
	else
		DrawSpriteScreenFX(3, FX_INK, screen.xcenter, 89)
	end if
	if object.value3 == 3
		DrawSpriteScreenFX(6, FX_INK, screen.xcenter, 129)
	else
		DrawSpriteScreenFX(5, FX_INK, screen.xcenter, 129)
	end if
	if object.value3 == 4
		DrawSpriteScreenFX(8, FX_INK, screen.xcenter, 169)
	else
		DrawSpriteScreenFX(7, FX_INK, screen.xcenter, 169)
	end if
	if object.value5 == 1
		DrawSpriteScreenFX(10, FX_INK, screen.xcenter, 43)
	else
		DrawSpriteScreenFX(9, FX_INK, screen.xcenter, 43)
	end if
	if object.value7 == 1
		DrawSpriteScreenFX(12, FX_INK, screen.xcenter, 43)
	else
		DrawSpriteScreenFX(11, FX_INK, screen.xcenter, 43)
	end if
	if object.value6 == 1
		DrawSpriteScreenFX(10, FX_INK, screen.xcenter, 83)
	else
		DrawSpriteScreenFX(9, FX_INK, screen.xcenter, 83)
	end if
	if object.value8 == 1
		DrawSpriteScreenFX(12, FX_INK, screen.xcenter, 83)
	else
		DrawSpriteScreenFX(11, FX_INK, screen.xcenter, 83)
	end if
	if object.value9 == 1
		DrawSpriteScreenFX(14, FX_INK, screen.xcenter, 123)
	else
		DrawSpriteScreenFX(13, FX_INK, screen.xcenter, 123)
	end if
	if object.value11 == 1
		DrawSpriteScreenFX(16, FX_INK, screen.xcenter, 123)
	else
		DrawSpriteScreenFX(15, FX_INK, screen.xcenter, 123)
	end if
	if options.timeLimit == 1
		DrawSpriteScreenFX(19, FX_INK, screen.xcenter, 123)
	else
		DrawSpriteScreenFX(20, FX_INK, screen.xcenter, 123)
	end if
	temp0 = object.value0
	temp0 >>= 1
	temp0 &= 1
	temp0 += 17
	if object.value13 == 1
		temp0 = 18
	end if
	DrawSpriteScreenFX(temp0, FX_INK, screen.xcenter, 195)
	if options.physicalControls == 1
		if object.value3 == 5
			if object.state != 4
				DrawSpriteScreenFX(21, FX_INK, screen.xcenter, 195)
			end if
		end if
	end if
	switch character.MenuCoice
	case 0
		DrawSpriteScreenFX(22, FX_INK, screen.xcenter, 41)
		DrawSpriteScreenFX(23, FX_INK, screen.xcenter, 41)
		break
	case 1
		DrawSpriteScreenFX(24, FX_INK, screen.xcenter, 41)
		break
	case 2
		DrawSpriteScreenFX(25, FX_INK, screen.xcenter, 41)
		break
	case 3
		DrawSpriteScreenFX(26, FX_INK, screen.xcenter, 41)
		break
	case 4
		DrawSpriteScreenFX(27, FX_INK, screen.xcenter, 41)
		DrawSpriteScreenFX(28, FX_INK, screen.xcenter, 41)
		break
	end switch
	switch options.bossMusic
	case 0
		DrawSpriteScreenFX(29, FX_INK, screen.xcenter, 86)
		break
	case 1
		DrawSpriteScreenFX(30, FX_INK, screen.xcenter, 86)
		break
	case 2
		DrawSpriteScreenFX(31, FX_INK, screen.xcenter, 86)
		break
	case 3
		DrawSpriteScreenFX(32, FX_INK, screen.xcenter, 86)
		break
	case 4
		DrawSpriteScreenFX(33, FX_INK, screen.xcenter, 86)
		break
	case 5
		DrawSpriteScreenFX(34, FX_INK, screen.xcenter, 86)
		break
	case 6
		DrawSpriteScreenFX(35, FX_INK, screen.xcenter, 86)
		DrawSpriteScreenFX(36, FX_INK, screen.xcenter, 86)
		break
	case 7
		DrawSpriteScreenFX(35, FX_INK, screen.xcenter, 86)
		DrawSpriteScreenFX(37, FX_INK, screen.xcenter, 86)
		break
	case 8
		DrawSpriteScreenFX(35, FX_INK, screen.xcenter, 86)
		DrawSpriteScreenFX(49, FX_INK, screen.xcenter, 86)
		break
	case 9
		DrawSpriteScreenFX(35, FX_INK, screen.xcenter, 86)
		DrawSpriteScreenFX(50, FX_INK, screen.xcenter, 86)
		break
	end switch
end event


event ObjectStartup
	ReadSaveRAM()
	options.bossMusic = saved.BossMusic
	character.MenuCoice = saved.Character
	options.timeLimit = saved.TimeLimit
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-52, 0, 104, 7, 103, 430) //0
//Player Text
	SpriteFrame(-82, 0, 48, 7, 158, 454)
	SpriteFrame(-82, 0, 48, 7, 207, 454)
//Music Text
	SpriteFrame(-82, 0, 40, 7, 103, 438)
	SpriteFrame(-82, 0, 40, 7, 103, 446)
//Time Limit
	SpriteFrame(-82, -4, 40, 15, 1, 485)
	SpriteFrame(-82, -4, 40, 15, 42, 485)
//Time
	SpriteFrame(-82, 0, 32, 7, 5, 485)
	SpriteFrame(-82, 0, 32, 7, 46, 485)

	SpriteFrame(-16, 0, 20, 20, 138, 22)
	SpriteFrame(-16, 0, 20, 20, 138, 64)	//10
	SpriteFrame(64, 0, 20, 20, 159, 22)
	SpriteFrame(64, 0, 20, 20, 159, 64)
	SpriteFrame(-8, 0, 40, 20, 1, 411)
	SpriteFrame(-8, 0, 40, 20, 42, 411)
	SpriteFrame(44, 0, 40, 20, 1, 432)
	SpriteFrame(44, 0, 40, 20, 42, 432)
	SpriteFrame(-20, 0, 40, 20, 1, 320)
	SpriteFrame(-20, 0, 40, 20, 42, 320)
	SpriteFrame(-10, -2, 42, 22, 1, 362)
	SpriteFrame(42, -2, 42, 22, 1, 362)	//20
	SpriteFrame(-22, -2, 42, 22, 1, 362)
	SpriteFrame(16, 0, 16, 23, 46, 386)
	SpriteFrame(36, 0, 16, 23, 63, 386)
	SpriteFrame(26, 0, 16, 23, 46, 386)
	SpriteFrame(26, 0, 16, 23, 63, 386)
	SpriteFrame(26, 0, 16, 23, 80, 386)
	SpriteFrame(16, 0, 16, 23, 80, 386)
	SpriteFrame(36, 0, 16, 23, 63, 386)
//Music Options Text
	SpriteFrame(26, 3, 16, 7, 83, 414)
	SpriteFrame(26, 3, 16, 7, 83, 422)	//30
	SpriteFrame(26, 3, 16, 7, 83, 430)
	SpriteFrame(26, 3, 16, 7, 83, 446)
	SpriteFrame(7, 3, 56, 7, 83, 454)
	SpriteFrame(7, 3, 56, 7, 83, 462)
	SpriteFrame(9, -1, 40, 15, 34, 461)

	SpriteFrame(54, 3, 8, 7, 34, 477)		//A
	SpriteFrame(54, 3, 8, 7, 43, 477)		//B
//Numbers
	SpriteFrame(0, 0, 8, 7, 175, 140)		//0
	SpriteFrame(0, 0, 8, 7, 183, 140)		//1
	SpriteFrame(0, 0, 8, 7, 191, 140)	//40	//2
	SpriteFrame(0, 0, 8, 7, 199, 140)		//3
	SpriteFrame(0, 0, 8, 7, 207, 140)		//4
	SpriteFrame(0, 0, 8, 7, 215, 140)		//5
	SpriteFrame(0, 0, 8, 7, 223, 140)		//6
	SpriteFrame(0, 0, 8, 7, 231, 140)		//7
	SpriteFrame(0, 0, 8, 7, 239, 140)		//8
	SpriteFrame(0, 0, 8, 7, 247, 140)		//9
// ;
	SpriteFrame(0, 0, 3, 7, 171, 140)		//;
//Music Text Part 2
	SpriteFrame(54, 3, 8, 7, 52, 477)		//C
	SpriteFrame(54, 3, 8, 7, 61, 477)	//50	//D
	
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
